package com.ruipeng.shengyapo.locksocket;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class LockServerListener implements ServletContextListener {
	
	private LockSocketThread lockSocketThread;

	@Override
	public void contextDestroyed(ServletContextEvent event) {
		if (lockSocketThread != null && !lockSocketThread.isInterrupted()) {
			lockSocketThread.interrupt();
		}
	}

	@Override
	public void contextInitialized(ServletContextEvent event) {
		ServletContext servletContext = event.getServletContext();
        System.out.println("Server contextInitialized over");
        if (lockSocketThread == null)
        {
        	lockSocketThread = new LockSocketThread(servletContext);
        	lockSocketThread.start(); 
        }
	}

}
